<?php

namespace app\service;

use app\model\BiliUserModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Log;

class UserService extends BaseService
{
    protected BiliUserModel $biliUserModel;
    protected function initialize(){
        $this->biliUserModel = new BiliUserModel();
    }
    public function userAdd(array $info,int $roomId): bool
    {
        echo "========================================",PHP_EOL;
        $num = count($info);
        $i = 1;
        foreach ($info as $key => $value) {
            echo "用户解析ing " . $i++ . "/{$num} {$value}",PHP_EOL;
            try {
                $userInfo = $this->biliUserModel->where("user_uuid", "=", $key)->find();
            } catch (ModelNotFoundException $e) {
                Log::error(date("Y-m-d H:i:s")."用户解析失败 未找到对应模型（biliUserModel） 详情:" . $e->getMessage());
                exit();
            } catch (DbException $e) {
                Log::error(date("Y-m-d H:i:s")."用户解析失败 数据库异常 详情:" . $e->getMessage());
                exit();
            }
            if($userInfo) {
                if($value != $userInfo->user_name) {
                    $data = [
                        "user_name" => $value,
                        "update_time" => date("Y-m-d H:i:s")
                    ];
                    try {
                        $result = $userInfo->save($data);
                        if($result) {
                            Log::info(date("Y-m-d H:i:s")." 更新用户 id: {$userInfo->user_id} info: ".json_encode($data,JSON_UNESCAPED_UNICODE));
                        }else {
                            Log::error(date("Y-m-d H:i:s")." 更新用户 失败 info:".json_encode($data,JSON_UNESCAPED_UNICODE));
                            exit();
                        }
                    }catch (\Exception $e){
                        Log::error(date("Y-m-d H:i:s")."更新用户失败 ".json_encode($data,JSON_UNESCAPED_UNICODE)." 详情:" . $e->getMessage());
                        exit();
                    }
                }
                $roomIdAttr = explode(",",$userInfo->room_id);
                if(!in_array($roomId,$roomIdAttr)) {
                    array_push($roomIdAttr,$roomId);
                    $data = [
                        "room_id" => implode(",",$roomIdAttr),
                        "update_time" => date("Y-m-d H:i:s")
                    ];
                    try {
                        $result = $userInfo->save($data);
                        if($result) {
                            Log::info(date("Y-m-d H:i:s")." 更新用户 id: {$userInfo->user_id} info: ".json_encode($data,JSON_UNESCAPED_UNICODE));
                        }else {
                            Log::error(date("Y-m-d H:i:s")." 更新用户 失败 info: ".json_encode($data,JSON_UNESCAPED_UNICODE));
                            exit();
                        }
                    }catch (\Exception $e){
                        Log::error(date("Y-m-d H:i:s")."更新用户失败 ".json_encode($data,JSON_UNESCAPED_UNICODE)." 详情:" . $e->getMessage());
                        exit();
                    }
                }
            }else {
                $data = [
                    "user_uuid"     => (int)$key,
                    "user_name"     => $value,
                    "room_id"       => $roomId,
                    "create_time"   => date("Y-m-d H:i:s"),
                    "update_time"   => date("Y-m-d H:i:s")
                ];
                try {
                    $id = $this->biliUserModel->insertGetId($data);
                    if($id) {
                        Log::info(date("Y-m-d H:i:s")." 插入用户 id: {$id} info: ".json_encode($data,JSON_UNESCAPED_UNICODE));
                    }else {
                        Log::error(date("Y-m-d H:i:s")." 插入用户 失败 info: ".json_encode($data,JSON_UNESCAPED_UNICODE));
                        exit();
                    }
                }catch (\Exception $e) {
                    Log::error(date("Y-m-d H:i:s")." 插入用户失败  ".json_encode($data,JSON_UNESCAPED_UNICODE)."  详情:" . $e->getMessage());
                    exit();
                }

            }
        }
        return true;
    }
    public function getUserIdList(): array
    {
        return $this->biliUserModel->column("id as user_id","user_uuid");
    }
}